<?php
/*
  $Id: sptrungcap.php $
  TomatoCart Open Source Shopping Cart Solutions
  http://www.tomatocart.com

  Copyright (c) 2009 Wuxi Elootec Technology Co., Ltd;  Copyright (c) 2006 osCommerce

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/

  class osC_Content_sptrungcap extends osC_Modules {
    var $_title,
        $_code = 'sptrungcap',
        $_author_name = 'ESC',
        $_author_www = 'http://esc.vn',
        $_group = 'content';

/* Class constructor */

    function osC_Content_sptrungcap() {
      global $osC_Language;

      $this->_title = $osC_Language->get('sptrungcap_title');
    }

    function initialize() {
      global $osC_Database, $osC_Services, $osC_Language, $osC_Currencies, $osC_Image, $osC_Specials, $current_category_id;

      if (MODULE_CONTENT_SPPHOTHONG_PARENT_CAT_ID < 1) {
        $Qnewproducts = $osC_Database->query('select p.products_id, p.products_tax_class_id, p.products_price, pd.products_name, pd.products_keyword, i.image, pd.products_short_description from :table_products p left join :table_products_images i on (p.products_id = i.products_id and i.default_flag = :default_flag), :table_products_description pd where p.products_status = 1 and p.products_id = pd.products_id and pd.language_id = :language_id  and p.products_price >= :limit_price_from_display_sptrungcap and p.products_price <= :limit_price_to_display_sptrungcap limit :max_display_sptrungcap');
      } else {
        $Qnewproducts = $osC_Database->query('select distinct p.products_id, p.products_tax_class_id, p.products_price, pd.products_name, pd.products_keyword, i.image, pd.products_short_description from :table_products p left join :table_products_images i on (p.products_id = i.products_id and i.default_flag = :default_flag), :table_products_description pd, :table_products_to_categories p2c, :table_categories c where c.parent_id = :parent_id and c.categories_id = p2c.categories_id and p2c.products_id = p.products_id and p.products_status = 1 and p.products_id = pd.products_id and pd.language_id = :language_id  and p.products_price >= :limit_price_from_display_sptrungcap and p.products_price <= :limit_price_to_display_sptrungcap limit :max_display_sptrungcap');
        $Qnewproducts->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
        $Qnewproducts->bindTable(':table_categories', TABLE_CATEGORIES);
        //$Qnewproducts->bindInt(':parent_id', $current_category_id);
		$Qnewproducts->bindInt(':parent_id', MODULE_CONTENT_SPTRUNGCAP_PARENT_CAT_ID);
      }

      $Qnewproducts->bindTable(':table_products', TABLE_PRODUCTS);
      $Qnewproducts->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
      $Qnewproducts->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
      $Qnewproducts->bindInt(':default_flag', 1);
      $Qnewproducts->bindInt(':language_id', $osC_Language->getID());
	  $Qnewproducts->bindInt(':limit_price_from_display_sptrungcap', MODULE_CONTENT_SPTRUNGCAP_PRICE_FROM_DISPLAY);
	  $Qnewproducts->bindInt(':limit_price_to_display_sptrungcap', MODULE_CONTENT_SPTRUNGCAP_PRICE_TO_DISPLAY);
      $Qnewproducts->bindInt(':max_display_sptrungcap', MODULE_CONTENT_SPTRUNGCAP_MAX_DISPLAY);

      if (MODULE_CONTENT_SPTRUNGCAP_CACHE > 0) {
        $Qnewproducts->setCache('sptrungcap-' . $osC_Language->getCode() . '-' . $osC_Currencies->getCode() . '-' . $current_category_id, MODULE_CONTENT_SPTRUNGCAP_CACHE);
      }

      $Qnewproducts->execute();

      if ($Qnewproducts->numberOfRows()) {
        $i = 0;
        while ($Qnewproducts->next()) {
          //if(($i % 5 == 0) && ($i != 0))
           // $this->_content .= '<div style="clear:both"></div>';

          $product = new osC_Product($Qnewproducts->valueInt('products_id'));
          
            $this->_content .= '<div class="productItem" >' .
                             '<span class="productName">' . osc_link_object(osc_href_link(FILENAME_PRODUCTS, $Qnewproducts->value('products_id')), $Qnewproducts->value('products_name')) . '</span>';
if($Qnewproducts->value('products_short_description')!=''){							 
         $this->_content .= osc_link_object(osc_href_link(FILENAME_PRODUCTS, $Qnewproducts->value('products_id')), $osC_Image->show($Qnewproducts->value('image'), $Qnewproducts->value('products_name')), 'id="productImage' . $Qnewproducts->value('products_id') . '" title="cssbody=[dvbdy1] cssheader=[dvhdr1] header=['.$Qnewproducts->value('products_name').'] body=['. nl2br($Qnewproducts->value('products_short_description')).'] "') ;}
		 else {
		 $this->_content .= osc_link_object(osc_href_link(FILENAME_PRODUCTS, $Qnewproducts->value('products_id')), $osC_Image->show($Qnewproducts->value('image'), $Qnewproducts->value('products_name')), 'id="productImage' . $Qnewproducts->value('products_id') . '"') ;		 
		 }
          $this->_content .= '<span  class="productPrice">' . $product->getPriceFormated(true) . '</span>' . 
                             osc_link_object(osc_href_link(FILENAME_PRODUCTS, $Qnewproducts->valueInt('products_id') . '&action=cart_add'), osc_draw_image_button('button_add_to_cart.png', $osC_Language->get('button_add_to_cart'), 'class="ajaxAddToCart" id="ac_newproductsmodule_' . $Qnewproducts->value('products_id') . '"')) . '</div>';

          $i++;
        }

        $this->_content .= '<div style="clear:both"></div>';
      }

      $Qnewproducts->freeResult();
    }

    function install() {
      global $osC_Database;

      parent::install();

      $osC_Database->simpleQuery("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum Entries To Display', 'MODULE_CONTENT_SPTRUNGCAP_MAX_DISPLAY', '9', 'Maximum number of new products to display', '6', '0', now())");
	  $osC_Database->simpleQuery("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Price From', 'MODULE_CONTENT_SPTRUNGCAP_PRICE_FROM_DISPLAY', '2000000', 'Limit price of products to show', '2000000', '0', now())");
	  $osC_Database->simpleQuery("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Price to', 'MODULE_CONTENT_SPTRUNGCAP_PRICE_TO_DISPLAY', '4999000', 'Limit price of products to show', '4999000', '0', now())");
	  $osC_Database->simpleQuery("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Parent Category ID', 'MODULE_CONTENT_SPTRUNGCAP_PARENT_CAT_ID', '2', 'Show all product from parent category want to set', '2', '0', now())");
      $osC_Database->simpleQuery("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Cache Contents', 'MODULE_CONTENT_SPTRUNGCAP_CACHE', '60', 'Number of minutes to keep the contents cached (0 = no cache)', '6', '0', now())");
    }

    function getKeys() {
      if (!isset($this->_keys)) {
        $this->_keys = array('MODULE_CONTENT_SPTRUNGCAP_MAX_DISPLAY','MODULE_CONTENT_SPTRUNGCAP_PRICE_FROM_DISPLAY', 'MODULE_CONTENT_SPTRUNGCAP_PRICE_TO_DISPLAY','MODULE_CONTENT_SPTRUNGCAP_PARENT_CAT_ID','MODULE_CONTENT_SPTRUNGCAP_CACHE');
      }

      return $this->_keys;
    }
  }
?>
